# == Schema Information
# Schema version: 38
#
# Table name: photos
#
#  id               :integer(11)     not null, primary key
#  parent_id        :integer(11)
#  content_type     :string(255)
#  filename         :string(255)
#  thumbnail        :string(255)
#  size             :integer(11)
#  width            :integer(11)
#  height           :integer(11)
#  user_id          :integer(11)
#  title            :string(255)
#  description      :text
#  created_at       :datetime
#  updated_at       :datetime
#  event_id         :integer(11)
#  school_id        :integer(11)
#  description_html :text
#  student_id       :integer(11)
#

class Photo < ActiveRecord::Base
  cattr_reader :per_page, :upload_amount
  @@per_page = 10
  @@upload_amount = 5
  
  format_attribute :description

  belongs_to :user
  has_many :comments, :class_name => "PhotoComment", :dependent => :destroy
  belongs_to :event
  belongs_to :school
  has_attachment :processor => :rmagick,
                 :content_type => :image,
                 :storage => :s3,
                 # :storage => :file_system,
                 :max_size => 2.megabytes,
                 :thumbnails => { :square => "75x75!",
                                  :thumb => "120x80>",
                                  :small => "240x180>",
                                  :medium => "565x420>" }
  validates_as_attachment
  
  def editable_by?(user)
    user!=:false && (user.id == user_id || user.is_admin)
  end
  
  def self.all
    self.find(:all, 
              :conditions => "photos.school_id<>0 and schools.is_hidden!=1", 
              :include    => 'school',
              :limit      => 4,
              :order      => "photos.created_at desc")
  end
  
  def self.last
    self.find(:first, :conditions => "parent_id is NULL", :order => "id DESC")
  end
  
  def previous
    Photo.find(:first, :conditions => ["parent_id is NULL and id < ?", self.id], :order => "id DESC")
  end
  
  def next
    Photo.find(:first, :conditions => ["parent_id is NULL and id > ?", self.id])
  end
end
